package com.example.mytestingapplication.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView lv = (ListView)findViewById(R.id.lv);

        Button but1 = (Button)findViewById(R.id.but1);
        Button but2 = (Button)findViewById(R.id.but2);

        final MyAdapter adapter = new MyAdapter();
        lv.setAdapter(adapter);

        // Onclick listener for button 1
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.clear();
                for (String a : getData(0))
                    adapter.mStrings.add(a);
                lv.setAdapter(adapter);
            }
        });

        // Onclick listener for button 2
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.clear();
                for (String a : getData(1))
                    adapter.mStrings.add(a);
                lv.setAdapter(adapter);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // This is the method that returns the list of strings based on the
    // integer index, in this case its the button (index 0 for button 1, index 1 for button 2)
    private String[] getData(int index) {
        if (index == 0)
            return new String[] {"test1", "test2", "test3"};
        else
            return new String[] {"test4", "test5", "test6"};
    }

    public static class MyAdapter extends BaseAdapter{

        // Create a list to contain the list of strings.
        final List<String> mStrings;

        // Constructor
        public MyAdapter() {
            mStrings = new ArrayList<String>();
        }

        // To clear the list
        public void clear(){
            mStrings.clear();
        }

        @Override
        public int getCount() {
            return mStrings.size();
        }

        @Override
        public Object getItem(int i) {
            if (i >= mStrings.size() || i < 0)
                return null;
            return mStrings.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View contextView, ViewGroup viewGroup) {
            View view = contextView;

            //In case the view is null, inflate the layout.
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater)viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.list_item, viewGroup, false);
            }

            TextView textView = (TextView)view.findViewById(R.id.text_item);
            textView.setText(mStrings.get(i));
            return view;
        }
    }
}
